
const router = require('koa-router')()
const fs = require("fs");
const path = require("path");
const uploadDir = path.join(__dirname, '../images/tags');
router.prefix('/tags')
//tags
//页面初始化时获取数据库内的数据条目
router.post('/gettagsbackend', async (ctx, next) => {
  let result = await ctx.db.EXCUTE('SELECT * FROM tags ORDER BY cropsid');
  ctx.body = {
    code: 200,
    data: result
  }
})

//编辑item 首先获取该项的内容
router.post('/getsinglebackend', async (ctx, next) => {
  let cropsid = ctx.request.body.cropsid;

  let sql = 'SELECT * FROM tags WHERE cropsid=?';
  let result = await ctx.db.EXCUTE(sql, [cropsid]);

  ctx.body = {
    code: 200,
    data: result
  }
})

router.post('/uploadimg', async (ctx, next) => {
  let cropsid = ctx.request.body.cropsid;
  let habit = ctx.request.body.habit;
  let suitable_area = ctx.request.body.suitable_area;
  let cropimg = ctx.request.body.cropimg;
  let cropname = ctx.request.body.cropname;
  let maxtemp = ctx.request.body.maxtemp;
  let mintemp = ctx.request.body.mintemp;
  let category = ctx.request.body.category;
  let stateday = ctx.request.body.stateday;
  let wettemp = ctx.request.body.wettemp;
  let ifsun = ctx.request.body.ifsun;
  let files = ctx.request.body.cropimg;
  let sql = 'UPDATE tags SET habit=?, suitable_area=?, cropname=?, maxtemp=?, mintemp=?, category=?, stateday=?, ifsun=?, wettemp=? WHERE cropsid=?'
  let result = await ctx.db.EXCUTE(sql, [habit, suitable_area, cropname, maxtemp, mintemp, category, stateday, ifsun, wettemp, cropsid]);

  console.log(ctx.request.body);
  ctx.body = {
    code: 200
  }
  if (files[0].url.indexOf('http://localhost:8080') != -1) {
    ctx.body = {
      code: 200
    }
  } else {
    let result = await ctx.db.EXCUTE("select cropname from tags where cropsid=?", [cropsid]);
    let n = result[0].cropname
    console.log(n);
    fs.unlink(path.join(__dirname, '../' + n), async function (err) {
      if (err) {
        console.log(err);
        ctx.body = {
          code: 500
        }
      }
    })

    var base64Data = files[0].url.replace(/^data:image\/\w+;base64,/, "");
    var dataBuffer = Buffer.from(base64Data, 'base64');
    let imgUrl = uploadDir + '\\' + files[0].name
    fs.writeFileSync(imgUrl, dataBuffer)
    let sql0 = 'UPDATE tags SET cropimg=? WHERE cropsid=?'
    await ctx.db.EXCUTE(sql0, ['/images/tags/' + files[0].name, cropsid]);
    ctx.body = {
      code: 200
    }
  }
});

router.post('/modifyuploadnoimgbackendtags', async (ctx, next) => {
  let cropsid = ctx.request.body.cropsid;
  let habit = ctx.request.body.habit;
  let suitable_area = ctx.request.body.suitable_area;
  let cropname = ctx.request.body.cropname;
  let maxtemp = ctx.request.body.maxtemp;
  let mintemp = ctx.request.body.mintemp;
  let category = ctx.request.body.category;
  let stateday = ctx.request.body.stateday;

  let sql = 'UPDATE tags SET habit=?, suitable_area=?, cropname=?, maxtemp=?, mintemp=?, category=?, stateday=? WHERE cropsid=?'
  let result = await ctx.db.EXCUTE(sql, [habit, suitable_area, cropname, maxtemp, mintemp, category, stateday, cropsid]);

  ctx.body = {
    code: 200,
    data: result
  }

});

//增加项
router.post('/addtagsbackend', async (ctx, next) => {
  let habit = ctx.request.body.habit;
  let suitable_area = ctx.request.body.suitable_area;
  let cropname = ctx.request.body.cropname;
  let maxtemp = ctx.request.body.maxtemp;
  let mintemp = ctx.request.body.mintemp;
  let category = ctx.request.body.category;
  let stateday = ctx.request.body.stateday;
  let wettemp = ctx.request.body.wettemp;
  let ifsun = ctx.request.body.ifsun;
  let files = ctx.request.body.cropimg;
  console.log(ctx.request.body);
  var base64Data = files[0].url.replace(/^data:image\/\w+;base64,/, "");
  var dataBuffer = Buffer.from(base64Data, 'base64');
  let imgUrl = uploadDir + '\\' + files[0].name
  fs.writeFileSync(imgUrl, dataBuffer)
  console.log(typeof (wettemp));
  console.log(wettemp, ifsun);
  let sql0 = 'INSERT INTO tags (habit,suitable_area,cropimg,cropname,maxtemp,mintemp,category, wettemp, ifsun, croptype) VALUES (?,?,?,?,?,?,?,?,?,?)'
  await ctx.db.EXCUTE(sql0, [habit, suitable_area, '/images/tags/' + files[0].name, cropname, maxtemp, mintemp, category, wettemp, ifsun, category]);

  let result = await ctx.db.EXCUTE('SELECT * FROM tags ORDER BY cropsid');
  ctx.body = {
    code: 200,
    data: result
  }

});

//add without pics
router.post('/addtagsnoimgbackend', async (ctx, next) => {
  let habit = ctx.request.body.habit;
  let suitable_area = ctx.request.body.suitable_area;
  let cropname = ctx.request.body.cropname;
  let maxtemp = ctx.request.body.maxtemp;
  let mintemp = ctx.request.body.mintemp;
  let category = ctx.request.body.category;
  let stateday = ctx.request.body.stateday;

  let sql0 = 'INSERT INTO tags (habit,suitable_area,cropname,maxtemp,mintemp,category) VALUES (?,?,?,?,?,?,?,?)'
  await ctx.db.EXCUTE(sql0, [habit, suitable_area, cropname, maxtemp, mintemp, category]);

  let result = await ctx.db.EXCUTE('SELECT * FROM tags ORDER BY cropsid');
  ctx.body = {
    code: 200,
    data: result
  }

});

//删除tags
router.post('/deltagsbackend', async (ctx, next) => {
  let cropsid = ctx.request.body.cropsid;
  let result0 = await ctx.db.EXCUTE("select cropimg from tags where cropsid=?", [cropsid]);
  let n = result0[0].cropimg
  fs.unlink(path.join(__dirname, '../' + n), async function (err) {
    if (err) {
      console.log(err);
      ctx.body = {
        code: 500
      }
    } else {
      console.log(200);

    }
  })
  let sql = 'DELETE FROM tags WHERE cropsid=?';
  await ctx.db.EXCUTE(sql, [cropsid]);
  ctx.body = {
    code: 200
  }
})


module.exports = router
